<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="Python是迄今为止最容易上手的编程语言之一。Python最大的优势之一在于它提供了无穷无尽强大的库。其中许多库核心使用C/C++编写，以提高运行/计算速度，并在顶部使用Python包装器以方便使用！ Numpy就是这样的一个Python库，Numpy主要用于数组操作和数据处理。 它的高速度和易于使用的性能使其成为数据科学和机器学习从业者的最爱。这篇文章将是一个Numpy实践教程——学习如何使用">
<meta property="og:type" content="article">
<meta property="og:title" content="The Easiest Python Numpy Tutorial Ever">
<meta property="og:url" content="http://yoursite.com/2019/06/28/The-Easiest-Python-Numpy-Tutorial-Ever/index.html">
<meta property="og:site_name" content="MARS">
<meta property="og:description" content="Python是迄今为止最容易上手的编程语言之一。Python最大的优势之一在于它提供了无穷无尽强大的库。其中许多库核心使用C/C++编写，以提高运行/计算速度，并在顶部使用Python包装器以方便使用！ Numpy就是这样的一个Python库，Numpy主要用于数组操作和数据处理。 它的高速度和易于使用的性能使其成为数据科学和机器学习从业者的最爱。这篇文章将是一个Numpy实践教程——学习如何使用">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-09-05T01:53:45.189Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="The Easiest Python Numpy Tutorial Ever">
<meta name="twitter:description" content="Python是迄今为止最容易上手的编程语言之一。Python最大的优势之一在于它提供了无穷无尽强大的库。其中许多库核心使用C/C++编写，以提高运行/计算速度，并在顶部使用Python包装器以方便使用！ Numpy就是这样的一个Python库，Numpy主要用于数组操作和数据处理。 它的高速度和易于使用的性能使其成为数据科学和机器学习从业者的最爱。这篇文章将是一个Numpy实践教程——学习如何使用">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/06/28/The-Easiest-Python-Numpy-Tutorial-Ever/">





  <title>The Easiest Python Numpy Tutorial Ever | MARS</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">MARS</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/06/28/The-Easiest-Python-Numpy-Tutorial-Ever/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="MARS">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/messi.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="MARS">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">The Easiest Python Numpy Tutorial Ever</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-06-28T15:41:14+08:00">
                2019-06-28
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Numpy/" itemprop="url" rel="index">
                    <span itemprop="name">Numpy</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/06/28/The-Easiest-Python-Numpy-Tutorial-Ever/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count gitment-comments-count" data-xid="/2019/06/28/The-Easiest-Python-Numpy-Tutorial-Ever/" itemprop="commentsCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>Python是迄今为止最容易上手的编程语言之一。Python最大的优势之一在于它提供了无穷无尽强大的库。其中许多库核心使用C/C++编写，以提高运行/计算速度，并在顶部使用Python包装器以方便使用！</p>
<p>Numpy就是这样的一个Python库，Numpy主要用于数组操作和数据处理。 它的高速度和易于使用的性能使其成为数据科学和机器学习从业者的最爱。这篇文章将是一个Numpy实践教程——学习如何使用Numpy！</p>
<a id="more"></a>
<p>Translated by <strong>Mars</strong> at 2019/7/28 11:30</p>
<h2 id="Creating-arrays"><a href="#Creating-arrays" class="headerlink" title="Creating arrays"></a>Creating arrays</h2><p>Numpy提供了几种创建数字型数组的方法。 如果有一些自定义好的数据，可以通过<code>numpy.array()</code>函数来构建Numpy数组。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np </span><br><span class="line"></span><br><span class="line"><span class="comment">### 通过给定数据构建Numpy数组，给定数据的类型可以是list或者tuple。</span></span><br><span class="line"><span class="comment">### 在使用np.array()构建数组的同时可以通过dtype=float/np.int16/...指定数组元素类型。</span></span><br><span class="line">np_array = np.array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>,  <span class="number">4</span>],</span><br><span class="line">                     [ <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>,  <span class="number">8</span>,  <span class="number">9</span>],</span><br><span class="line">                     [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>, <span class="number">14</span>]])</span><br><span class="line">np_array = np.array([(<span class="number">1.5</span>, <span class="number">2</span>, <span class="number">3</span>), </span><br><span class="line">                     (<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>)], </span><br><span class="line">                    dtype=float)</span><br><span class="line"></span><br><span class="line"><span class="comment">### 在没有给定数据的情况下，我们可以通过一些初始化函数来构建并初始化Numpy数组，以便后面使用。</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个3x4的全0数组</span></span><br><span class="line">np.zeros((<span class="number">3</span>, <span class="number">4</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个2x3x4的全1数组，并且数组元素类型为16位int。</span></span><br><span class="line">np.ones((<span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>), dtype=np.int16)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个2x3的空数组</span></span><br><span class="line">np.empty((<span class="number">2</span>, <span class="number">3</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个1维数组，数组元素从10到30，间隔为5</span></span><br><span class="line">np.arange(<span class="number">10</span>, <span class="number">30</span>, <span class="number">5</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个1维数组，数组元素从0到2，间隔为0.3</span></span><br><span class="line">np.arange(<span class="number">0</span>, <span class="number">2</span>, <span class="number">0.3</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建一个由0到2等间隔的9个数字的1维数组</span></span><br><span class="line">np.linspace(<span class="number">0</span>, <span class="number">2</span>, <span class="number">9</span>)</span><br></pre></td></tr></table></figure>
<h2 id="Getting-array-information"><a href="#Getting-array-information" class="headerlink" title="Getting array information"></a>Getting array information</h2><p>创建数组后，可以获取有关它的一些基本信息。由于数组已经作为Numpy数组加载，因此可以直接使用数组的属性。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取数组维度大小</span></span><br><span class="line">np_array.ndim</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取数组维度信息</span></span><br><span class="line">np_array.shape</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取数组元素个数</span></span><br><span class="line">np_array.size</span><br><span class="line"></span><br><span class="line"><span class="comment"># 获取数组元素类型详细信息</span></span><br><span class="line">np_array.dtype</span><br></pre></td></tr></table></figure>
<h2 id="Basic-Arithmetic"><a href="#Basic-Arithmetic" class="headerlink" title="Basic Arithmetic"></a>Basic Arithmetic</h2><p>无论对数据进行任何数学计算，Numpy都将是一个很好的选择。</p>
<p>Numpy几乎可以执行任何通常应用于其他Python对象（列表，集合等）的基本数学运算——但速度更快！</p>
<p>许多Numpy的数学函数都是用经过优化的C编写的，这比任何其他Python实现要快得多。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在Numpy中，数组的数学计算通常是针对数组元素进行操作</span></span><br><span class="line"><span class="comment"># A simple example </span></span><br><span class="line"><span class="comment"># Note: 保证做减法运算的两个数组大小相同</span></span><br><span class="line">a = np.array( [<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>] )</span><br><span class="line">b = np.array( [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] )</span><br><span class="line">c = a - b</span><br><span class="line">c = [<span class="number">20</span>, <span class="number">29</span>, <span class="number">38</span>, <span class="number">47</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 幂运算</span></span><br><span class="line">b**<span class="number">2</span></span><br><span class="line">b = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 正弦函数</span></span><br><span class="line"><span class="number">10</span> * np.sin(a)</span><br><span class="line">a = [ <span class="number">9.12945251</span>, <span class="number">-9.88031624</span>,  <span class="number">7.4511316</span> , <span class="number">-2.62374854</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 数组点乘</span></span><br><span class="line">a = np.array( [<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>] )</span><br><span class="line">b = np.array( [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>] )</span><br><span class="line">c = a * b</span><br><span class="line">c = [<span class="number">0</span>, <span class="number">30</span>, <span class="number">80</span>, <span class="number">150</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># Numpy常用的一些函数</span></span><br><span class="line">a = np.array( [<span class="number">20</span>,<span class="number">30</span>,<span class="number">40</span>,<span class="number">50</span>] )</span><br><span class="line">a.max() <span class="comment"># 50</span></span><br><span class="line">a.min() <span class="comment"># 20</span></span><br><span class="line">a.sum() <span class="comment"># 140</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Numpy构建多维数组，并按维度进行取值或者操作</span></span><br><span class="line">b = np.arange(<span class="number">12</span>).reshape(<span class="number">3</span>,<span class="number">4</span>)</span><br><span class="line">b = np.array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>],</span><br><span class="line">              [ <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>],</span><br><span class="line">              [ <span class="number">8</span>,  <span class="number">9</span>, <span class="number">10</span>, <span class="number">11</span>]])</span><br><span class="line"></span><br><span class="line"><span class="comment"># Note: 0-纵向  1-横向</span></span><br><span class="line">b.sum(axis=<span class="number">0</span>) <span class="comment"># [12, 15, 18, 21]</span></span><br><span class="line">b.min(axis=<span class="number">1</span>) <span class="comment"># [0, 4, 8]</span></span><br><span class="line"><span class="comment"># 对每行进行累加 </span></span><br><span class="line">b.cumsum(axis=<span class="number">1</span>) <span class="comment"># [[ 0,  1,  3,  6], [ 4,  9, 15, 22], [ 8, 17, 27, 38]]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建序列数组</span></span><br><span class="line">b = np.arange(<span class="number">3</span>)</span><br><span class="line">b = [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>]</span><br><span class="line"></span><br><span class="line"><span class="comment"># e^x</span></span><br><span class="line">np.exp(b) <span class="comment"># [ 1.0, 2.71828183, 7.3890561 ]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 平方根</span></span><br><span class="line">np.sqrt(b) <span class="comment"># [ 0.0 ,  1.0, 1.41421356]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 返回不大于输入参数的最大整数</span></span><br><span class="line">np.floor(np.exp(b)) <span class="comment"># [ 1.0, 2.0, 7.0 ]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 四舍五入</span></span><br><span class="line">np.round(np.exp(b)) <span class="comment"># [ 1.0, 3.0, 7.0 ]</span></span><br></pre></td></tr></table></figure>
<h2 id="Array-Slicing-and-Shaping"><a href="#Array-Slicing-and-Shaping" class="headerlink" title="Array Slicing and Shaping"></a>Array Slicing and Shaping</h2><p>切片是一种非常常见的Python操作，使用Python列表相对容易。 但是当尝试对大型多维列表进行切片时，很容易造成混乱。</p>
<p>Numpy通过为所有Numpy数组提供非常简单直观的切片来节省时间。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Numpy数组可以像Python列表一样被索引，切片，遍历</span></span><br><span class="line">a = np.array([<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>, <span class="number">10</span>])</span><br><span class="line">a[<span class="number">2</span>] <span class="comment"># 2</span></span><br><span class="line">a[<span class="number">2</span>:<span class="number">5</span>] <span class="comment"># [2, 3, 4]</span></span><br><span class="line">a[<span class="number">-1</span>] <span class="comment"># 10</span></span><br><span class="line">a[:<span class="number">8</span>] <span class="comment"># [0, 1, 2, 3, 4, 5, 6, 7]</span></span><br><span class="line">a[<span class="number">2</span>:] <span class="comment"># [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Numpy多维数组</span></span><br><span class="line">b = np.array([[ <span class="number">0</span>,  <span class="number">1</span>,  <span class="number">2</span>,  <span class="number">3</span>],</span><br><span class="line">              [<span class="number">10</span>, <span class="number">11</span>, <span class="number">12</span>, <span class="number">13</span>],</span><br><span class="line">              [<span class="number">20</span>, <span class="number">21</span>, <span class="number">22</span>, <span class="number">23</span>],</span><br><span class="line">              [<span class="number">30</span>, <span class="number">31</span>, <span class="number">32</span>, <span class="number">33</span>],</span><br><span class="line">              [<span class="number">40</span>, <span class="number">41</span>, <span class="number">42</span>, <span class="number">43</span>]])</span><br><span class="line"></span><br><span class="line">b[<span class="number">2</span>, <span class="number">3</span>] <span class="comment"># 23</span></span><br><span class="line">b[<span class="number">0</span>:<span class="number">5</span>, <span class="number">1</span>] <span class="comment"># each row in the second column of b --&gt; [ 1, 11, 21, 31, 41]</span></span><br><span class="line">b[ : , <span class="number">1</span>] <span class="comment"># same thing as above --&gt; [ 1, 11, 21, 31, 41]</span></span><br><span class="line">b[<span class="number">1</span>:<span class="number">3</span>, : ] <span class="comment"># all column from 1 to 3 of row --&gt; [[10, 11, 12, 13], [20, 21, 22, 23]]</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 遍历行</span></span><br><span class="line"><span class="keyword">for</span> row <span class="keyword">in</span> b:</span><br><span class="line">  print(row)</span><br><span class="line"><span class="comment"># [0 1 2 3]</span></span><br><span class="line"><span class="comment"># [10 11 12 13]</span></span><br><span class="line"><span class="comment"># [20 21 22 23]</span></span><br><span class="line"><span class="comment"># [30 31 32 33]</span></span><br><span class="line"><span class="comment"># [40 41 42 43]</span></span><br></pre></td></tr></table></figure>
<h2 id="More-interesting-tips-and-tricks"><a href="#More-interesting-tips-and-tricks" class="headerlink" title="More interesting tips and tricks"></a>More interesting tips and tricks</h2><p>掌握上面的Numpy基础，就可以开始探索一些更高级的功能。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">### Numpy数据类型</span></span><br><span class="line"></span><br><span class="line">np.int64 <span class="comment"># Signed 64-bit integer types</span></span><br><span class="line">np.float32 <span class="comment"># Standard double-precision floating point</span></span><br><span class="line">np.complex <span class="comment"># Complex numbers represented by 128 floats</span></span><br><span class="line">np.bool <span class="comment"># Boolean type storing TRUE and FALSE values</span></span><br><span class="line">np.object <span class="comment"># Python object type</span></span><br><span class="line">np.string <span class="comment"># Fixed-length string type</span></span><br><span class="line">np.unicode <span class="comment"># Fixed-length unicode type</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### Numpy arrays可以直接通过关系运算符进行比较，返回boolean类型的值</span></span><br><span class="line">a = np.array([<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>])</span><br><span class="line">b = np.array([<span class="number">5</span>, <span class="number">4</span>, <span class="number">3</span>])</span><br><span class="line">a == b <span class="comment"># array([False, False, True])</span></span><br><span class="line">a &lt;= <span class="number">2</span> <span class="comment"># array([False, True, True])</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 比较整个数组</span></span><br><span class="line">np.array_equal(a, b) <span class="comment"># False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 根据维度排序</span></span><br><span class="line">c = np.array([[<span class="number">2</span>, <span class="number">4</span>, <span class="number">8</span>], [<span class="number">1</span>, <span class="number">13</span>, <span class="number">7</span>]])</span><br><span class="line">c.sort(axis=<span class="number">0</span>) <span class="comment"># array([[1, 4, 7], [2, 13, 8]])</span></span><br><span class="line">c.sort(axis=<span class="number">1</span>) <span class="comment"># array([[2, 4, 8], [1, 7, 13]])</span></span><br><span class="line"></span><br><span class="line"><span class="comment">### Numpy内置函数</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 转置</span></span><br><span class="line">d = np.transpose(c)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 重构</span></span><br><span class="line">c.ravel() <span class="comment"># This flattens the array 展开</span></span><br><span class="line">c.reshape((<span class="number">3</span>, <span class="number">2</span>)) <span class="comment"># Reshape the array from (2, 3) to (3, 2)</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 添加/移除元素</span></span><br><span class="line">np.append(c, d) <span class="comment"># 将c，d展开flattens，然后将d添加到c的末尾</span></span><br><span class="line">np.insert(a, <span class="number">1</span>, <span class="number">5</span>, axis=<span class="number">0</span>) <span class="comment"># Insert the number '5' at index 1 on axis 0</span></span><br><span class="line">np.delete(a, [<span class="number">1</span>], axis=<span class="number">1</span>) <span class="comment"># Delete item at index 1, axis 1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 拼接 arrays</span></span><br><span class="line">np.concatenate((c, d),axis=<span class="number">0</span>)  <span class="comment"># axis=0——纵向拼接，等同于 np.vstack</span></span><br><span class="line">np.concatenate((c, d),axis=<span class="number">1</span>)  <span class="comment"># axis=1——横向拼接，等同于 np.hstack</span></span><br><span class="line">np.vstack((c, d))  <span class="comment"># 纵向 垂直</span></span><br><span class="line">np.hstack((c, d))  <span class="comment"># 横向 水平</span></span><br></pre></td></tr></table></figure>
<h2 id="Reference"><a href="#Reference" class="headerlink" title="Reference"></a>Reference</h2><p>Translated from <a href="https://towardsdatascience.com/the-easiest-python-numpy-tutorial-ever-5c206c809a0d" target="_blank" rel="noopener">The Easiest Python Numpy Tutorial Ever</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/07/03/The-ABCs-of-PyTorch-Tensor/" rel="prev" title="The ABCs of PyTorch Tensor">
                The ABCs of PyTorch Tensor <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      
        <div id="gitment-container"></div>
      
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/messi.jpg" alt="MARS">
            
              <p class="site-author-name" itemprop="name">MARS</p>
              <p class="site-description motion-element" itemprop="description">自由是一种信仰</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">11</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">4</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/JyZhang10Mars" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="jyzhang.mars@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Creating-arrays"><span class="nav-number">1.</span> <span class="nav-text">Creating arrays</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Getting-array-information"><span class="nav-number">2.</span> <span class="nav-text">Getting array information</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Basic-Arithmetic"><span class="nav-number">3.</span> <span class="nav-text">Basic Arithmetic</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Array-Slicing-and-Shaping"><span class="nav-number">4.</span> <span class="nav-text">Array Slicing and Shaping</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#More-interesting-tips-and-tricks"><span class="nav-number">5.</span> <span class="nav-text">More interesting tips and tricks</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Reference"><span class="nav-number">6.</span> <span class="nav-text">Reference</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">MARS</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,Douban,QQZone,Twitter,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  
  


  

  

</body>
</html>
